let prices = [2, 1, 2, 0, 1]
var maxProfit = function (prices) {
    let left = 0
    let right = 1
    let maxP = 0
    let arr = []
    while (right < prices.length) {
        if (prices[left] < prices[right]) {
            if (prices[right - 1] <= prices[right]) {
                maxP = Math.max(maxP, prices[right] - prices[left])
                right++
            } else {
                arr.push(maxP)
                left = right
                right++
                maxP = 0
            }
        }
        if (prices[left] === prices[right] || prices[left] > prices[right]) {
            left++
            right++

        }
        console.log(prices[left], prices[right]);
    }
    arr.push(maxP)
    // console.log(arr);

    let sum = 0
    arr.forEach(item => sum += item)
    return sum

};
console.log(maxProfit(prices));
